package com.bj58.data.emailreports.utils;

import java.util.HashMap;

import com.bj58.wf.mvc.BeatContext.Model;

public class TemplateMethodFactory {

	private static HashMap<String, Object> pool = new HashMap<String, Object>();
	
	@SuppressWarnings("unchecked")
	public static <T> T getInstance(Class<T> c){
		T methodModel = null;
		try{
			if(pool.containsKey(c.getName())){
				methodModel = (T) pool.get(c.getName());
			}else{
				methodModel = (T) Class.forName(c.getName()).newInstance();
				pool.put(c.getName(), methodModel);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return methodModel;
	}
	
	 @SuppressWarnings({ "unchecked", "rawtypes" })
	public static void setVelocityContext(String key, Class c, Model model) {
		// Single
		Object value = TemplateMethodFactory.getInstance(c);
		model.add(key, value);
	}
}

